layui.define(['element','layer','form','eleTree','common','jquery'],function(exports){
    var $ = layui.$
        ,element = layui.element
        ,layer = layui.layer
        ,form = layui.form
        ,eleTree = layui.eleTree
        ,common = layui.common;
    //初始传入参数
    var _config = {};
    var el5;
    var view = {
        init:function(config){
            _config = config;
            form.verify({
                pass: [
                    /^[\S]{2,25}$/
                    ,'密码必须2到25位，且不能出现空格'
                ],
                username:function(value,item){
                    var msg;
                    if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                        msg = '用户名不能有特殊字符';
                    }
                    if(/(^\_)|(\__)|(\_+$)/.test(value)){
                        msg = '用户名首尾不能出现下划线\'_\'';
                    }
                    if(/^\d+\d+\d$/.test(value)){
                        msg = '用户名不能全为数字';
                    }
                    $.ajax({
                        type: "POST",
                        url:  "/system/user/checkLoginNameUnique",
                        async:false,
                        cache:false,
                        dataType: "json",
                        data: {
                            loginName:$("[name='loginName']").val()
                        },
                        success: function(res){
                            if(res != "0"){
                                msg = "登录用户名已存在，请修改！";
                            }
                        },
                        error:function(){
                            msg = "验证登录名出错！";
                        }
                    });
                    return msg;
                }
            });

            $("[name='deptName']").on("click",function (e) {
                e.stopPropagation();
                if(!el5) {
                    el5 = eleTree.render({
                        elem: '.deptName',
                        url:   "/system/dept/treeData",
                        request: {
                            name: "name",
                            key: "id",
                            children: "children",
                            checked: "checked",
                            disabled: "disabled",
                            isLeaf: "isLeaf"
                        },
                        response: {
                            statusName: "code",
                            statusCode: 0,
                            dataName: "data"
                        },
                        defaultExpandAll: true,
                        expandOnClickNode: false,
                        highlightCurrent: true
                    });
                    $(".eleTree").css("position","absolute");
                }
                $(".deptName").toggle();
            });

            eleTree.on("nodeClick(deptNameTree)",function(d) {
                $("[name='deptName']").val(d.data.currentData.name);
                $("[name='deptId']").val(d.data.currentData.id);
                $(".deptName").hide();
            })

            form.on('switch(switchTest)', function(data){
                if(this.checked){
                   $("status").val("0");
                }else{
                    $("status").val("1");
                }

            });
            form.render(null, 'form-user');
            form.on('submit(btn-add)', function(data){
                var roleIds = common.getCheckValues("role");
                var postIds = common.getCheckValues("post");
                data.field.roleIds = roleIds;
                data.field.postIds = postIds;
                data.field.status = data.field.status === "on" ? "1":"0";

                console.log(JSON.stringify(data.field));

                //TODO:提供一个检测表单数据检测性通用方法
                if(true){
                    $.ajax({
                        cache : false,
                        type : "POST",
                        url :   "/system/user/add",
                        data : data.field,
                        async : false,
                        error : function(request) {
                            layui.msg("系统错误");
                        },
                        success : function(data) {
                            console.log("success:",data);
                            if(data.code > 0 ){
                                layui.msg(data.msg);
                            }else{
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                parent.layui.list.refreshTable();
                            }
                        }
                    });
                }
                return false;
            });
        },
        submit:function(config){
            var iframeWindow = window['layui-layer-iframe'+ config.index];
            var _form = $(window.frames["layui-layer-iframe" + config.index].document).contents().find("#btn-add");
            $(_form).click();
        }
    };

    exports("add",view);
});